package com.krtech.wecard.module.sys.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.krtech.wecard.framework.model.CommRes;
import com.krtech.wecard.module.sys.entity.SysSchoolInfo;
import com.krtech.wecard.module.sys.entity.SysUser;
import com.krtech.wecard.module.sys.pojo.SysSchoolInfoVo;
import com.krtech.wecard.module.sys.service.SysDeptService;
import com.krtech.wecard.module.sys.service.SysSchoolInfoService;
import com.krtech.wecard.module.sys.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Api(tags = "学校信息操作")
@RestController
@RequestMapping("/sys/school")
@Slf4j
public class SysSchoolInfoController {

    @Autowired
    private SysSchoolInfoService schoolInfoService;

    @Autowired
    private SysDeptService deptService;

    @Autowired
    private SysUserService sysUserService;

    /**
     * 增
     */
    @PutMapping("/add")
    @ApiOperation(value = "增")
    public CommRes add(@RequestBody SysSchoolInfo sysSchoolInfo){
        return schoolInfoService.add(sysSchoolInfo);
    }

    /**
     * 改
     */
    @PostMapping("/update")
    @ApiOperation(value = "改")
    public CommRes update(@RequestBody SysSchoolInfo sysSchoolInfo){
        schoolInfoService.updateById(sysSchoolInfo);
        return CommRes.ok(sysSchoolInfo);
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除")
    public CommRes delete(Integer id){

        return CommRes.ok(schoolInfoService.removeById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public CommRes page(Page<SysSchoolInfo> page,  String keyword) {
        QueryWrapper<SysSchoolInfo> wrapper = new QueryWrapper<>();

        if (StrUtil.isNotEmpty(keyword)){
            wrapper.like(SysSchoolInfo.COL_NAME,keyword)
                    .or()
                    .like(SysSchoolInfo.COL_SCHOOL_CODE,keyword);
        }


        Page<SysSchoolInfo> schoolInfoPage = schoolInfoService.page(page, wrapper);

        return CommRes.ok(schoolInfoPage);
    }

    /**
     * getById
     * @return
     */
    @GetMapping("/getById")
    @ApiOperation(value = "getById")
    public CommRes<SysSchoolInfoVo> getById(Integer id) {
        SysSchoolInfo schoolInfo = schoolInfoService.getById(id);
        SysSchoolInfoVo sysSchoolInfoVo = new SysSchoolInfoVo();
        BeanUtil.copyProperties(schoolInfo,sysSchoolInfoVo);

        int count = sysUserService.count(new QueryWrapper<SysUser>());

        sysSchoolInfoVo.setCurrentSchoolNumber(count);

        return CommRes.ok(sysSchoolInfoVo);
    }




}
